package lhy.orm;

import lhy.frontend.annotation.EntityComment;
import lhy.orm.annotation.BasicBean;
import lhy.orm.annotation.Column;
import lhy.orm.annotation.TreeStructure;
import lhy.orm.annotation.ViewOnly;
import lhy.lhyorm.field.DatabaseField;
import lhy.lhyorm.table.DatabaseTable;
import lhy.utils.ReflectUtils;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class OrmUtils {
    public static String getEntityComment(Class<?> entClz){
        return entClz.getAnnotation(EntityComment.class).value();
    }
    public static String getEntityTableName(Class<?> clz){
        return clz.getAnnotation(DatabaseTable.class).tableName();
    }
    public static boolean isFieldHasForeignKey(Field field){
        return field.getAnnotation(DatabaseField.class).foreign();
        /*ForeignKeyMapping mapping = field.getAnnotation(ForeignKeyMapping.class);
        if(mapping==null) return  null;
        String fname = field.getName();
        String srcColumn = mapping.source();
        String srcTable = getEntityTableName(field.getDeclaringClass());
        String srcClz = ReflectUtils.getClassFullName(field.getDeclaringClass()) ;
        String targetColumn = mapping.target();;
        String targetTable = field.getType().getSimpleName();
        String targetTableFuN = ReflectUtils.getClassFullName(field.getType());
        return new ForeignKey(fname,srcColumn,srcTable,srcClz,targetColumn,targetTable,targetTableFuN);*/
    }
    public static boolean isFieldAutoIncrement(Field field){
        return Optional.of(field.getAnnotation(DatabaseField.class).generatedId()).orElse(false);
    }
    public static boolean isFieldIdentity(Field field){
        return Optional.of(field.getAnnotation(DatabaseField.class).id()).orElse(false);
    }
    public static boolean isEntityViewOnly(Class<?> clz){
        return clz.getAnnotation(ViewOnly.class)!=null;
    }
    public static boolean isFieldNullable(Field field){
        return Optional.of(field.getAnnotation(DatabaseField.class).canBeNull()).orElse(false);
    }
    public static boolean isClassBasicBean(Class<?> clazz){
        return clazz.getAnnotation(BasicBean.class)!=null;
    }
    public static boolean isClassTreeStructure(Class<?> clazz){
        return clazz.getAnnotation(TreeStructure.class)!=null;
    }
    public static String getFieldDescription(Field field){
        return field.getAnnotation(Column.class).value();
    }

    public static List<String> getEntityFieldDescriptions(Class<?> modelClass){
        return Arrays.stream(ReflectUtils.getAccessibleFields(modelClass))
                .map(OrmUtils::getFieldDescription)
                .collect(Collectors.toList());
    }
    //对象 每个属性 值 list (有序)、
    public static List<Object> getEntityFieldValues(Object modelInstance,Class<? extends Serializable> modelClass){
        return Arrays.stream(ReflectUtils.getAccessibleFields(modelClass))
                .map(field -> ReflectUtils.getFieldValue(field, modelInstance))
                .collect(Collectors.toList());
    }
    public static Field getIdentityField(Class<? > modelClass) {
    	return Arrays.stream(ReflectUtils.getAccessibleFields(modelClass))
                .filter(f -> f.getAnnotation(DatabaseField.class).id()||f.getAnnotation(DatabaseField.class).generatedId()).toList().get(0);
    }
}
